//
//  TYPersonInfoVC.m
//  Meum
//
//  Created by 黄卫 on 17/2/27.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "TYPersonInfoVC.h"

@interface TYPersonInfoVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIDatePicker *datePicker;
@property(nonatomic,strong)UITextField *textF;
@property(nonatomic,strong)UIToolbar *toolBar;
@end

@implementation TYPersonInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
    
    
}



-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UITableView groupedTableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UITextField *)textF{
    if (!_textF) {
        _textF = [[UITextField alloc] init];
        _textF.delegate = self;
        _textF.inputView = [self datePicker];
        _textF.inputAccessoryView = [self toolBar];
        CGFloat x = [UIScreen mainScreen].bounds.size.width-80-30;
        _textF.frame = CGRectMake(x, 0, 80, 20);
        _textF.text = @"1990-12-12";
        
    }
    return _textF;
}

-(UIToolbar *)toolBar{
    if (!_toolBar) {
        _toolBar = [[UIToolbar alloc] init];
        _toolBar.barTintColor = [UIColor grayColor];
        _toolBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
        
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
        UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sure:)  ];
        _toolBar.items = @[item1,item2,item3];
        
    }
    return _toolBar;
}

-(void)cancel:(UIBarButtonItem *)item{
    
}

-(void)sure:(UIBarButtonItem *)item{
    
}

-(UIDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _datePicker;
}

-(void)dateChange:(UIDatePicker *)picker{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat= @"yyyy-MM-dd";
    self.textF.text = [formatter stringFromDate: picker.date];
}

-(void)tap:(UITapGestureRecognizer *)tap{
    [self.textF resignFirstResponder];
}

#pragma mark UITextFieldDelegate
/**
 是否允许输入框编辑
 */
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return NO;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

#pragma mark UItTableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row==0) {
        cell.textLabel.text = @"hello";
        
        UIImageView *imgView = [[UIImageView alloc] init];
        CGFloat x = [UIScreen mainScreen].bounds.size.width-50-30;
        imgView.frame = CGRectMake(x, 0, 50, 50);
        imgView.image = [UIImage imageNamed:@"icon_home1"];
        [cell.contentView addSubview:imgView];
    }else if(indexPath.row==1){
        cell.textLabel.text = @"姓名";
        cell.detailTextLabel.text = @"黄先生";
    }else if(indexPath.row==2){
        cell.textLabel.text = @"性别";
        cell.detailTextLabel.text = @"男";
    }else if(indexPath.row ==3){
        cell.textLabel.text = @"出生年月";
        [cell.contentView addSubview:self.textF];
        
        
    }else if (indexPath.row ==4){
        cell.textLabel.text = @"证件";
        cell.detailTextLabel.text = @"大陆居民身份证";
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==0) {
        WEAKSELF
        [GJAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
            
        } title:@"hello1" message:@"hello2" cancelButtonName:@"cancel" otherButtonTitles:@"ok", nil];
    }else if(indexPath.row==1){
        WEAKSELF
        [GJAlertView actionSheetBCallBlock:^(NSInteger buttonIndex) {
            
        } cancelBlock:^{
            
        } WithTitle:@"hello1" cancelButtonTitle:@"hello2" otherButtonTitleArr:@[@"hello3"]];
    
    }else if (indexPath.row ==2){
        
    }else if (indexPath.row ==3){
        
    }else if (indexPath.row ==4){
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
